package listStudy;

import common.utils.TreeNode;

public class IsBalanced {

    private boolean res = true;

    public boolean IsBalanced_Solution(TreeNode root) {
        dfs(root);
        return res;
    }

    public int dfs(TreeNode root){
        if (root == null){
            return 0;
        }
        if (res){
            int left = dfs(root.left);
            int right = dfs(root.right);
            if (Math.abs(left - right) > 1){
                res = false;
            }
            return 1 + Math.max(left, right);
        }
        return 0;
    }


}
